package com.linuxgt.web.controller;

import com.linuxgt.web.mybatis.mapper.UserMapper;
import com.linuxgt.web.mybatis.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * mysql数据库测试控制器
 */

@Controller("com.linuxgt.web.controller.MysqlTestController")
@RequestMapping("/testdb/*")
public class MysqlTestController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(getClass());
    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/userInsert")
    @ResponseBody
    public Map<String, Object> insertUser() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        User user = new User("liuteng");
        userMapper.insertUser(user);
        log.debug("user:{}", user);
        resultMap.put("user", user);
        return resultMap;
    }

    /**
     * 查询单个用户信息
     * @param param
     * @return
     */
    @RequestMapping(value = "/userQuery", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryUser(@RequestBody Map<String, Object> param) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String id = (String) param.get("id");
        User user = userMapper.selectUserById(id);
        log.debug("user:{}", id);
        resultMap.put("data", user);
        return resultMap;
    }
}
